package com.pansoft.openplanet.activity;

import android.os.Bundle;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.util.PrivateKeyUtils;
import com.pansoft.openplanet.widget.ExportPrivateKeyDialog;
import com.pansoft.openplanet.widget.PassWordAlertDialog;
import com.utilcode.util.ToastUtils;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 安全加固，如备份私钥等
 * Created by will on 18-5-2.
 */

public class SafetyArchitectureActivity extends TCBaseActivity implements View.OnClickListener{

    private String filePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_safety_architecture);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_security_hardening));
        if (getIntent().getExtras() != null) {
            filePath = getIntent().getExtras().getString("filePath");
        }
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        ConstraintLayout clBackupPrivKey = (ConstraintLayout) findViewById(R.id.cl_backup_private_key);
        clBackupPrivKey.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.cl_backup_private_key) {
            PassWordAlertDialog dialog = new PassWordAlertDialog(SafetyArchitectureActivity.this, new PassWordAlertDialog.OnEnterClick() {
                @Override
                public void passVerifySuccess(String passWord) {
                    coverPrimaryKey();
                }
            });
            dialog.show();
        }
    }

    //备份私钥
    private void coverPrimaryKey() {
        Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                String key = PrivateKeyUtils.exportPrivateKey(filePath, "");
                emitter.onNext(key);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                showLoading(ResStringUtil.getString(R.string.open_planet_generating_private_key));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                dismissLoading();
                ExportPrivateKeyDialog dialog = new ExportPrivateKeyDialog(SafetyArchitectureActivity.this);
                dialog.setMessage(s);
                dialog.show();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                dismissLoading();
                ToastUtils.showShort(R.string.open_planet_get_private_key_fail);
            }
        });

        addDisposable(disposable);
    }
}
